<?php
@header("content-Type: text/html; charset=utf-8"); 
authCodeCheck(); 
function authCodeCheck()
{ 
	// 测试 直接return 
	return;
	$auth = 0; 
	$pubkey = 'q3KcEr7lhXr7GJ2V8H9nl8R58eMCyfOf';
	$data = read_static_cache('auth'); 
	$host = isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME']; 
	$host = str_replace('www.','',$host); 
	$encode = md5(md5($host . AuthKey . $pubkey)); 

	if($data !== false)
	{ 
		$data['auth'] = explode('|',$data['auth']); 
		$data['auth'] = array_filter($data['auth']); 
	}

	if ($data === false || !in_array($encode, $data['auth']))
	{ 
		$authUrl = 'http://auth.aiwopai.com/api/authorization'; 
		$postData = array( 'key' => AuthKey, 'domain' => $host, 'version' => Version, 'server_software' => $_SERVER['SERVER_SOFTWARE'], 'server_addr' => $_SERVER['SERVER_ADDR'], 'document_root' => $_SERVER['DOCUMENT_ROOT'], 'server_admin' => $_SERVER['SERVER_ADMIN'], 'php_version' => PHP_VERSION, 'php_os' => PHP_OS, ); 
		$data_curl = array(); 
		$data_curl = curlPost($postData, $authUrl); 
		$data_curl = json_decode($data_curl, true);
		if(!empty($data_curl) && $encode==$data_curl['encode'] && $data_curl['status']==1)
		{ 
			$data['auth'][] = $encode; 
			write_static_cache('auth',array('auth'=>implode('|',$data['auth']),'time'=>time())); 
			$auth = 1;
		}
	}
	else
	{ 
		if(!empty($data['auth']) && in_array($encode, $data['auth'])){ $auth = 1; } 
	} 

	if($auth == 0)
	{ 
		die('您的程序未经过官方授权，请联系我们客服!'); 
	} 
} 

function safe_verify($user, $username, $password)
{ 
	$result = false; 
	if($user)
	{ 
		if(get_salt_hash($password, $user['salt']) == $user['password'])
		{ 
			$result = true; 
		} 
	} 

	if($result)
	{ 
		$_SESSION['uname'] = $user['username']; 
		$_SESSION['uid'] = $user['uid']; 
		$_SESSION['gid'] = $user['group_id']; 
		$_SESSION['skin'] = '1'; 
		$_SESSION['vor'] = ($user['group_id']>0 && isset($user['visitor'])) ? intval($user['visitor']) : 0; 
	} 

	return $result; 
} 

function get_salt_hash($password, $salt, $gsalt='scYltK') 
{
	$passwordmd5 = preg_match('/^\w{32}$/', $password) ? $password : md5($password); 
	return md5($passwordmd5.$salt.$gsalt); 
} 

function curlPost($curlPost,$url)
{ 
	$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); 
	curl_setopt($curl, CURLOPT_HEADER, false); 
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
	curl_setopt($curl, CURLOPT_TIMEOUT, 60); 
	curl_setopt($curl, CURLOPT_NOBODY, true); 
	curl_setopt($curl, CURLOPT_POST, true); 
	curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); 
	$return_str = curl_exec($curl); 
	curl_close($curl); 
	return $return_str; 
} 

function httpVerify()
{ 
	$host = isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME']; 
} 

function get($key)
{ 
	return isset($_GET[$key]) ? $_GET[$key] : ''; 
} 

function post($key)
{ 
	return isset($_POST[$key]) ? $_POST[$key] : ''; 
}

function &loadClass($class, $directory = '', $prefix = 'Lowxp_')
{ 
	static $_classes = array(); 
	if (isset($_classes[$class])) 
		return $_classes[$class]; 

	$name = false; 
	foreach (array(AppDir, SysDir) as $path)
	{ 
		if (is_file($path.$directory.'/'.$class.'.php'))
		{ 
			$name = $prefix.$class; 
			if (class_exists($name) === false)
			{ 
				require($path.$directory.'/'.$class.'.php'); 
			} 

			break; 
		} 
	} 

	if($name === false) 
		exit('Unable to locate the specified class: '.$class.'.php'); 

	isLoaded($class); 
	$_classes[$class] = new $name(); 

	return $_classes[$class]; 

} 

function &isLoaded($class = '')
{ 
	static $_is_loaded = array(); 
	if ($class != '') 
		$_is_loaded[strtolower($class)] = $class; 

	return $_is_loaded; 
} 

function URL_404()
{ 
	ShowError('',404); 
} 

function ShowError($message, $code=200, $heading='运行出现错误')
{ 
	$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>'; 

	SetHeader($code,is_array($message)?'':$message); 
	ob_start(); 

	if($code==404)
	{ 
		include(RootDir.'system/error/404.php');
	}
	else
	{ 
		include(RootDir.'system/core/general.php'); 
	} 

	$buffer = ob_get_contents(); 
	ob_end_clean(); 
	echo $buffer; 
} 

function SetHeader($code=200,$text='')
{ 
	$status = array( 200=>'OK',
		201=>'Created',
		202=>'Accepted',
		203=>'Non-Authoritative Information',
		204=>'No Content',
		205=>'Reset Content',
		206=>'Partial Content',
		300=>'Multiple Choices',
		301=>'Moved Permanently',
		302=>'Found',
		304=>'Not Modified',
		305=>'Use Proxy',
		307=>'Temporary Redirect',
		400=>'Bad Request',
		401=>'Unauthorized',
		403=>'Forbidden',
		404=>'Not Found',
		405=>'Method Not Allowed',
		406=>'Not Acceptable',
		407=>'Proxy Authentication Required',
		408=>'Request Timeout',
		409=>'Conflict',
		410=>'Gone',
		411=>'Length Required',
		412=>'Precondition Failed',
		413=>'Request Entity Too Large',
		414=>'Request-URI Too Long',
		415=>'Unsupported Media Type',
		416=>'Requested Range Not Satisfiable',
		417=>'Expectation Failed',
		500=>'Internal Server Error',
		501=>'Not Implemented',
		502=>'Bad Gateway',
		503=>'Service Unavailable',
		504=>'Gateway Timeout', 
		505=>'HTTP Version Not Supported' 
		); 

	if($code=='' || !is_numeric($code))
	{ 
		ShowError('Status codes must be numeric',500); 
	} 

	if(isset($status[$code]) AND $text=='')
	{ 
		$text = $status[$code]; 
	} 

	if($text=='')
	{ 
		ShowError('No status text available.  Please check your status code number or supply your own message text.',500); 
	} 

	$server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : false; 

	if(substr(php_sapi_name(),0,3) == 'cgi')
	{ 
		header("Status: {$code} {$text}", true); 
	}
	elseif($server_protocol == 'HTTP/1.1' || $server_protocol == 'HTTP/1.0')
	{ 
		header($server_protocol." {$code} {$text}",true,$code);
	}
	else
	{ 
		header("HTTP/1.1 {$code} {$text}",true,$code); 
	} 
} 

function ErrorHandle($errno, $errstr, $errfile, $errline)
{ 
	$Error = array( 
		1=>array('ERROR','致命错误，脚本执行中断,就是脚本中有不可识别的东西出现'),
		2=>array('WARINIG','部分代码出错,但不影响整体运行'),
		4=>array('PARSE','字符、变量或结束的地方写规范有误'),
		8=>array('NOTICE','一般通知，如变量未定义等'),
		16=>array('CORE_ERROR','PHP进程在启动时,发生了致命性错误'),
		32=>array('CORE_WARNING','在PHP启动时警告(非致命性错误)'),
		64=>array('COMPILE_ERROR','编译时致命性错误'),
		128=>array('COMPILE_WARNING','编译时警告级错误'),
		256=>array('USER_ERROR','用户自定义的错误消息'),
		512=>array('USER_WARNING','用户自定义的警告消息'),
		1024=>array('USER_NOTICE','用户自定义的提醒消息'),
		2048=>array('E_STRICT','不包括E_STRICT的所有的报错信息'),
		4096=>array('RECOVERABLE_ERROR','编码标准化警告,允许PHP建议如何修改代码以确保最佳的互操作性向前兼容性。'),
		8191=>array('ALL','不包括E_STRICT的所有的报错信息') 
		); 

	ShowError(array( 
		'错误级别：'.(isset($Error[$errno])?$Error[$errno][0]:'未知错误').','.$Error[$errno][1], 
		'出错文件： '.$errfile, '行号： '.$errline, '错误描述：'.$errstr )
	); 
} 

if(!function_exists('array_column'))
{ 
	function array_column(array $array, $column, $index_key = null) 
	{ 
		$columns = array(); 

		foreach($array as $key=>$value) 
		{ 
			if(!is_array($array[$key]) || empty($array[$key])) 
			{ 
				continue; 
			} 

			if($index_key===null) 
			{ 
				if(isset($array[$key][$column])) 
				{ 
					$columns[] = $array[$key][$column]; 
				} 
			}
			else
			{ 
				if(isset($array[$key][$index_key]) && isset($array[$key][$column])) 
				{ 
					$columns[$array[$key][$index_key]] = $array[$key][$column]; 
				} 
			} 
		} 

		return $columns; 
	} 
} 

function debuger($start=1)
{ 
	if(!IS_DEV)
	{ 
		return; 
	}; 

	$data = debug_backtrace(); 

	if(is_array($data))
	{ 
		echo 'DeBug调试:程序堆栈.<br>';
		$i = 0; 
		foreach($data as $val)
		{ 
			if($i++<$start)
				continue; 

			if(isset($val['type']))
			{ 
				if(isset($val['class']))
				{ 
					echo '<span style="color:#666">调用方法:</span><span style="color:#090">'.$val['class'].'</span>'.$val['type'].'<span style="color:blue">'.$val['function']."</span><br>"; 

					debug_getargs($val); 
					debug_showpos($val); 
				} 
			}
			else
			{ 
				echo '<span style="color:#666">调用方法:</span><span style="color:blue">'.$val['function']."</span><br>"; 

				debug_getargs($val); 
				debug_showpos($val); 
			} 

			echo '<hr>'; 
		} 
	} 
} 

function debug_getargs($val)
{ 
	if(!empty($val['args']) && is_array($val['args']))
	{ 
		$i = 0; 
		foreach($val['args'] as $v)
		{ 
			echo '参数'.(++$i).':'; 

			if(is_string($v))
			{ 
				echo $v; 
			} 

			if(is_array($v))
			{ 
				if(count($v)==0)
				{ 
					echo 'type(Array)Empty;'; 

				}
				else
				{ 
					echo 'type(Array)'; 
					foreach($v as $k=>$arg)
					{ 
						if(is_object($arg))
						{ 
							echo '['.$k.']=><span style="color:#888">[object]</span>'.'<b style="color:#f60">'.get_class($arg).'</b>'; 
						} 

						if(is_string($arg))
						{ 
							echo '['.$k.']=><span style="color:#888">[string]</span><span style="color:#444">'.$arg.'</span>'; 
						} 

						if(is_array($arg))
						{ 
							echo '<span style="color:#888">array:</span>'; 
							echo '<pre>';
							print_r($arg);
							echo '</pre>'; 
						} 

						echo ','; 
					} 
				} 
			} 
			echo '<br>'; 
		} 
	} 
} 

function debug_showpos($val)
{ 
	if(isset($val['file']))
	{ 
		echo '<span style="color:#999">位置:'.$val['file'].",第".$val['line']."行</span><br>"; 
	} 
}

function com_format($data)
{ 
	if(is_numeric($data))
	{ 
		$error = $this->load->config('error','api'); 
		if(isset($error[$data]))
		{ 
			$data = array( 'code' => $data, 'message' => $error[$data], ); 
		}
		else
		{ 
			$data = array( 'code' => $data ); 
		} 
	}
	elseif(is_array($data))
	{ 
		$data['code'] = 0; 
	}
	elseif(is_string($data))
	{ 
		$data = array( 'code' => 0, 'message' => $data, ); 
	} 

	$data['serverTime'] = RUN_TIME; 

	echo json_encode($data); 
	die; 
} 

function com_tip($message, $options = array('type'=>0,'hideWin'=>false,'inIframe'=>false))
{ 
	$defaultOpt = array( 'type'=>0, 'hideWin'=>false, 'time'=>3, 'inIframe'=>false, 'hideoverlay'=>false ); 
	$options = array_merge($defaultOpt, $options); 
	$optJson = json_encode($options); 
	foreach($options as $k=>$v)
		$options[strtolower($k)] = $v; 

	$parent = ($options['iniframe']?'parent.':''); 
	echo '<script type="text/javascript">'; 
	echo $parent.'com.xtip("'.$message.'",'.$optJson.');'; 

	if($options['hidewin'])
		echo $parent.'com.xhide();'; 

	echo '</script>'; 
} 


function com_refresh($time = 3, $isParent = false)
{ 
	$code = 'location.href=location.href;'; 
	if($isParent)
		$code = "parent.location.href=parent.location.href;"; 

	$this->exeJs('setTimeout(function(){'.$code.'},'.($time*1000).')'); 
} 

function com_exeJs($javascript_code)
{ 
	echo '<script type="text/javascript">'.$javascript_code.'</script>'; 
}

?>